/*
 * Created on October 10, 2009, 2:39 PM
 *
 * @author Wang Juan
 * @version 1.0
 * @since 1.0
 *
 */
package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.controller.AdvertiserController;
import sg.edu.nus.iss.ooad.jass.entity.Advertiser;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 * @author  ht082200U
 */
public class JASSUpdateAdvertiserForm extends AbstractJASSRegularWindow {

        private static final Logger logger = Logger.getLogger(JASSUpdateAdvertiserForm.class.getName());
        private static final long serialVersionUID = -2267351625029212775L;

        private Advertiser advertiser = null;
        private AbstractJASSRegularWindow parent ;

       /**
        * Contructor of JASSUpdateAdvertiserForm class
        * @param parent
        */
        public JASSUpdateAdvertiserForm( AbstractJASSRegularWindow parent) {
                super("Job Advertisement and Search System", new Dimension(550, 400));
                this.parent = parent;
                initComponents();
                initData();
        }

      /**
       * Set advertiser object
       * @param advertiser
       */
        public void setAdvertiser(Advertiser advertiser) {
                this.advertiser = advertiser;
        }

        /**
         * Get advertiser object
         * @return
         */
        public Advertiser getAdvertisr() {
                return this.advertiser;
        }
/**
 * init the advertiser object.
 */
        private void initData() {
                advertiser = new Advertiser();
        }

        /**
         * This method is called from within the constructor to
         * initialize the form.
         * 
         */
        private void initComponents() {

                labelError = new Label();
                userName = new Label("Name");
                textName = new TextField(35);
                password = new Label("Password");
                textPassword = new TextField(15);
                textPassword.setEchoChar('*');


                btnSave = new Button("Save");
                btnCancel = new Button("Cancel");


                //set panel
                buttonPanel = new Panel();
                inputPanel = new Panel();
                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                inputLayout = new GridBagLayout();
                inputPanel.setLayout(inputLayout);


                buttonPanel.add(btnSave);
                buttonPanel.add(btnCancel);

                //set actionlistener
                btnSave.addActionListener(new UpdateActionListener());
                btnCancel.addActionListener(new CancelActionListener());


                attachComponent(labelError, 0, 0, 2, 1);
                attachComponent(userName, 0, 2, 1, 1);
                attachComponent(textName, 1, 2, 1, 1);
                attachComponent(password, 0, 3, 1, 1);
                attachComponent(textPassword, 1, 3, 1, 1); 


                formPanel = new JASSRegularPanel("JASS Update Advertiser Form");
                formPanel.add(inputPanel, BorderLayout.CENTER);
                formPanel.add(buttonPanel, BorderLayout.SOUTH);

                //add to the frame
                add(formPanel);

                pack();
        }


        
/**
 * Display the form
 */
        public void display() {
                textName.setText(advertiser.getName());
                textPassword.setText(advertiser.getPassword());

        }
        // Variables declaration - do not modify
        private Button btnCancel;
        private Button btnSave;
        private Label labelError;
        private Label userName;
        private Label password;
        private TextField textName;
        private TextField textPassword;
        private JASSRegularPanel formPanel = null;
        private Panel inputPanel = null;
        private Panel buttonPanel = null;
        private GridBagLayout inputLayout = null;

        // End of variables declaration
        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                GridBagConstraints constraint = new GridBagConstraints();
                constraint.gridx = gridx;
                constraint.gridy = gridy;
                constraint.gridwidth = gridwidth;
                constraint.gridheight = gridheight;
                constraint.fill = fill;
                constraint.insets = new Insets(4, 4, 4, 4);
                constraint.weightx = 1.0;
                constraint.weighty = 1.0;
                inputPanel.add(c);
                inputLayout.setConstraints(c, constraint);
        }
/**
 * Reset the form
 */
        @Override
        public void resetForm() {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         *Update the advertiser action
         */
        class UpdateActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        AdvertiserController advertController = new AdvertiserController();

                        String userName = textName.getText();
                        String password = textPassword.getText();


                        if (userName == null || "".equalsIgnoreCase(userName)) {
                                labelError.setText("Please provide User Name");
                                return;
                        }

                        if (password == null || "".equalsIgnoreCase(password)) {
                                labelError.setText("Please provide User Password");
                                return;
                        }

                        try {
                                advertiser.setName(textName.getText());
                                advertiser.setPassword(textPassword.getText());
                                advertController.updateAdvertiser(advertiser);

                        } catch (Exception ex) {
                                logger.log(Level.SEVERE, null, ex);
                        }
                }
        }

        /**
         *Exit from the form 
         */
        class CancelActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                         close();
                }
        }

       
}
